package cn.audit.controller.organization;

import cn.audit.aop.myannotation.BussAnnotation;
import cn.audit.bean.dto.BatchProcessing;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.AuditInstitution;
import cn.audit.service.institution.InstitutionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 李忆枫
 * @Date: 2019/5/29 11:40
 * <p>
 * 审计机构管理
 */
@Controller
@RequestMapping("/AuditManagement")
public class AuditManagementController {

    @Resource
    private InstitutionService institutionService;

    @RequestMapping("/audit")
    public String audit(Model model) {

        List<UserCorrelation> institutionAll = institutionService.findAuditInstitutionAll();
        model.addAttribute("institutionAll", institutionAll);

        return "organization/audit";
    }

    @ResponseBody
    @RequestMapping("/AjaxFindSubordinate")
    public Map<String, Object> AjaxFindSubordinate(Integer id) {
        //创建一个map，用来封装数据
        Map map = new HashMap(16);

        System.out.println("---------------------------------");
        List<AuditInstitution> superior = institutionService.findBySuperior(Long.valueOf(id));
        System.out.println(superior + "--------------------");

        map.put("superior", superior);
        return map;
    }

    @ResponseBody
    @RequestMapping("/AjaxById")
    public Map<String, Object> AjaxById(Integer id) {
        //创建一个map，用来封装数据
        Map map = new HashMap(16);
        AuditInstitution byId = institutionService.findAuditInstitutionById(Long.valueOf(id));
        map.put("byId", byId);
        return map;
    }

    @RequestMapping("/save")
    @BussAnnotation(moduleName = "审计机构管理", type = "添加", option = "添加机构成功")
    public String save(Integer superior, String code, String auditname) {
        AuditInstitution auditInstitution = new AuditInstitution();
        auditInstitution.setAuditname(auditname);
        auditInstitution.setSuperior(Long.valueOf(superior));
        auditInstitution.setCode(code);
        institutionService.save(auditInstitution);
        return "redirect:audit";
    }

    @ResponseBody
    @RequestMapping("/BatchRemove")
    @BussAnnotation(moduleName = "审计机构管理", type = "删除", option = "删除机构成功")
    public Boolean BatchRemove(BatchProcessing ids) {
        List<Long> id = ids.getIds();
        institutionService.BatchRemove(id);
        return true;
    }

    @RequestMapping("/batchUpdate")
    public String batchUpdate(String code, String auditname, Integer id) {

        institutionService.batchUpdate(code, auditname, Long.valueOf(id));

        return "redirect:audit";
    }

}
